import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { AppConfig } from "../config";
import { Role } from "../entity/role.model";
import { Observable } from "rxjs/Observable";
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class RoleService {
  private readonly param = "sysRoles";
  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  constructor(private http: InterceptorService) {
  }


  /**
   * 获取所有的角色
   * 
   * @returns {Promise<UserManage[]>} 
   * @memberof UserService
   */
  getRoles(): Promise<Role[]> {
    return this.http.get(`${this.url}`)
      .toPromise()
      .then(res => res.json().data as Role[])
      .catch(this.handleError);
  }


  /**
   * 删除
   * 
   * @param {number[]} valueIds 
   * @returns {Promise<boolean>} 
   * @memberof RoleService
   */
  public batchDelete(valueIds: number[]): Promise<any> {
    const apiUrl = `${this.url}?items=${valueIds}`;
    return this.http.delete(apiUrl)
      .toPromise()
      .then(response => {
        try {
          let result = response.json();
          switch (result.code) {
            case "200":
              return result;
            case "8001":
              return result;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          throw error;
        }

      })
      .catch(this.handleError);
  }


  /**
   * 新增
   * 
   * @param {Role} role 
   * @returns {Promise<Role>} 
   * @memberof RoleService
   */
  create(role: Role): Promise<any> {
    // const Url = `http://192.168.15.150:8585/webapi/v1/sysRoles`
    return this.http.post(this.url, JSON.stringify(role))
      .toPromise()
      .then(res => {
        try {
          let result = res.json();
          switch (result.code) {
            case "200":
              return result.code as Role;
            case "-1":
              throw new Error(result.message);
          }
        }
        catch (error) {
          this.handleError(error);
        }
      })
      .catch(this.handleError);
  }


  /**
   * 更新
   * 
   * @param {Role} role 
   * @returns {Promise<Role>} 
   * @memberof RoleService
   */
  update(role: Role): Promise<Role> {
    const url = `${this.url}`;
    return this.http.put(url, JSON.stringify(role))
      .toPromise()
      .then(() => role)
      .catch(this.handleError);
  }



  /**
   * 异步校验角色名称是否重复
   * 
   * @param {string} roleName 
   * @param {number} roleId 
   * @returns {Observable<boolean>} 
   * @memberof RoleService
   */
  hasRoleName(roleName: string, roleId: number): Observable<boolean> {
    const url = `${this.url}/roleName/${roleName}roleId/${roleId}`;
    // const url = `http://192.168.15.150:8585/webapi/v1/sysRoles/roleName/${roleName}/roleId/${roleId}`;
    return this.http.get(url).map(response => {
      let code = response.json().code;
      switch (code) {
        case "3001":
          console.log("hasRoleName3001")
          return true;
        case "3002":
          console.log("hasRoleName3002")
          return false;
      }
    }).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}









// WEBPACK FOOTER //
// ./src/app/service/role.service.ts